<template>
  <div>
    <div class="header">
      <el-button
        type="primary"
        @click="dialogFormVisible = true"
        class="add"
        size="small"
        >新增
      </el-button>
    </div>
    <el-table :data="noticelist" style="width: 100%" stripe border :default-sort = "{prop: 'date', order: 'descending'}">
      <el-table-column label="标题" width="360" prop="name">
      </el-table-column>
      <el-table-column label="发布时间" width="360" sortable prop="date">
      </el-table-column>
      <el-table-column label="发布人" width="360" prop="people">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="新增公告" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" :label-width="formLabelWidth">
              <el-input
                v-model="form.name"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="发布人" :label-width="formLabelWidth">
              <el-input v-model="form.people" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公告内容" :label-width="formLabelWidth">
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第一段文字"
                v-model="form.content1"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第二段文字"
                v-model="form.content2"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第三段文字"
                v-model="form.content3"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第四段文字"
                v-model="form.content4"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第五段文字"
                v-model="form.content5"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12"> </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClub">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改公告" :visible.sync="editFormVisible" width="40%">
      <el-form :model="form">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" :label-width="formLabelWidth">
              <el-input
                v-model="editform.name"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="发布人" :label-width="formLabelWidth">
              <el-input v-model="editform.people" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公告内容" :label-width="formLabelWidth">
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第一段文字"
                v-model="editform.content1"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第二段文字"
                v-model="editform.content2"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第三段文字"
                v-model="editform.content3"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第四段文字"
                v-model="editform.content4"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="4"
                placeholder="第五段文字"
                v-model="editform.content5"
                maxlength="150"
                show-word-limit
                resize="none"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12"> </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClub">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { v4 as uuidv4 } from "uuid";
var dayjs = require("dayjs");
// import dayjs from 'dayjs' // ES 2015
dayjs().format();
export default {
  name: "MyNotice",
  mounted() {
    this.all();
  },
  data() {
    return {
      addDate: "",
      deleteid: "",
      noticelist: [],
      tableData: [],
      editFormVisible: false,
      dialogFormVisible: false,
      form: {
        id: "",
        name: "",
        people: "",
        date: "",
        content1: "",
        content2: "",
        content3: "",
        content4: "",
        content5: "",
      },
      editform: {
        id: "",
        name: "",
        people: "",
        date: "",
        content1: "",
        content2: "",
        content3: "",
        content4: "",
        content5: "",
      },
      formLabelWidth: "80px",
    };
  },
  methods: {
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editform = row;
    },
    handleDelete(index, row) {
      this.deleteid = row.id;
      this.del();
    },
    formatter(row, column) {
      console.log(row, column);
      return row.address;
    },
    addClub() {
      if (
        this.form.name == "" ||
        this.form.people == "" ||
        this.form.content1 == ""
      ) {
        alert("请填写完整信息(公告名称、发布人、公告内容)后再点击确定");
      } else {
        var now = dayjs();
        this.addDate = now.$y + "-" + (now.$M + 1) + "-" + now.$D;
        this.dialogFormVisible = false;
        this.form.id = uuidv4();
        this.add();
        this.form = {
          id: "",
          name: "",
          people: "",
          date: "",
          content1: "",
          content2: "",
          content3: "",
          content4: "",
          content5: "",
        };
      }
    },
    editClub() {
      // console.log(this.editform);
      if (
        this.editform.name == "" ||
        this.editform.people == "" ||
        this.editform.content1 == ""
      ) {
        alert("请填写完整信息(公告名称、发布人、公告内容)后再点击确定");
      } else {
        this.update();
        this.editFormVisible = false;
      }
    },
    all() {
      //查找info表全部数据
      axios
        .get("http://127.0.0.1/noticelist_yang/all")
        .then((res) => {
          // console.log(res.data);
          this.noticelist = res.data;
          // console.log(this.noticelist);
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    add() {
      //添加操作
      axios
        .get("http://127.0.0.1/noticelist_yang/add", {
          params: {
            id: this.form.id,
            name: this.form.name.trim(),
            people: this.form.people.trim(),
            date: this.addDate,
            content1: this.form.content1.trim(),
            content2: this.form.content2.trim(),
            content3: this.form.content3.trim(),
            content4: this.form.content4.trim(),
            content5: this.form.content5.trim(),
          },
        })
        .then((res) => {
          console.log(res);
          this.all();
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    del() {
      //删除操作
      axios
        .get("http://127.0.0.1/noticelist_yang/del", {
          params: {
            id: this.deleteid,
          },
        })
        .then((res) => {
          console.log(res.data);
          this.all();
        })
        .catch((err) => {
          console.log("操作失败" + err);
        });
    },
    update() {
      //修改操作
      axios
        .get("http://127.0.0.1/noticelist_yang/update", {
          params: {
            id: this.editform.id,
            name: this.editform.name.trim(),
            people: this.editform.people.trim(),
            date: this.editform.date,
            content1: this.editform.content1.trim(),
            content2: this.editform.content2.trim(),
            content3: this.editform.content3.trim(),
            content4: this.editform.content4.trim(),
            content5: this.editform.content5.trim(),
          },
        })
        .then((res) => {
          console.log(res);
          this.all();
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
  },
};
</script>

<style scoped>
.header {
  height: 56px;
  width: 100%;
  background-color: #1e874c;
}
.add {
  margin-top: 10px;
  margin-left: 20px;
}
</style>
